#include <iostream>
#include <string>
using namespace std;



bool isPrime(int n) {
    if (n <= 1) {
        return false;
    }
    for (int i = 2; i * i <= n; ++i) {
        if (n % i == 0) {
            return false;
        }
    }
    return true;
}   
int main() {
    int number;
    cin>>number;
    if (number < 1000 || number > 9999) {
        cout << "0";
        return 1;
    }
    int max = 0;
       
    string str_num, k, l, m, n;
    str_num = to_string(number);
        
    k = str_num[0];
    l = str_num[1];
    m = str_num[2];
    n = str_num[3];
        
    int n1, n2, n3, n4, n5, n6, n7, n8;
        
    n1 = stoi(k);
    n2 = stoi(l);
    n3 = stoi(m);
    n4 = stoi(n);
        
    n5 = stoi(k+l);
    n6 = stoi(m+n);
    n7 = stoi(k+l+m);
    n8 = stoi(l+m+n);
    
    if (isPrime(number) && number > max) {
        max = number;}
        
    if (isPrime(n1) && n1 > max) {
        max = n1;}
        
    if (isPrime(n2) && n2 > max) {
        max = n2;}
        
    if (isPrime(n3) && n3 > max) {
        max = n3;}
        
    if (isPrime(n4) && n4> max) {
        max = n4;}
        
    if (isPrime(n5) && n5 > max) {
        max = n5;}
        
    if (isPrime(n6) && n6 > max) {
        max = n6;}
        
    if (isPrime(n7) && n7 > max) {
        max = n7;}
        
    if (isPrime(n8) && n8 > max) {
        max = n8;}
        
        cout << max;
}
